using System;
using System.Text;
using System.Globalization;
using System.Data;
using System.Collections.Generic;
using Microsoft.Practices.CompositeWeb;
using Microsoft.Practices.ObjectBuilder;
using WCSFContrib.ECommerceCatalog.BusinessEntities;
using WCSFContrib.ECommerceCatalog.CatalogData;
using WCSFContrib.ECommerceCatalog.User.Interfaces.Services;
using WCSFContrib.ECommerceCatalog.Product.Interfaces.Services;

namespace WCSFContrib.ECommerceCatalog.User.Services
{
    public class CartService : ICartService
    {
        private CatalogDataSet _catalogDataSet;
        private IItemsService _itemsService;

        public CartService([ServiceDependency] IItemsService itemsService)
            : this(CatalogRepository.Instance, itemsService)
        { }

        public CartService(CatalogDataSet ds, IItemsService itemsService)
        {
            _catalogDataSet = ds;
            _itemsService = itemsService;
        }

        public ICollection<Cart> AllCarts
        {
            get
            {
                ICollection<Cart> carts = new List<Cart>();
                foreach (CatalogDataSet.CartRow row in _catalogDataSet.Cart.Rows)
                {
                    carts.Add(TranslateFromCartRowToCartEntity(row));
                }
                return carts;
            }
        }

        public Cart GetCartByUserName(string userName)
        {
            CatalogDataSet.CartRow cartRow = _catalogDataSet.Cart.FindByUserName(userName);
            Cart cart;

            if (cartRow == null)
                return null;

            cart = TranslateFromCartRowToCartEntity(cartRow);

            return cart;
        }

        public void AddItem(string userName, int itemId, int quantity)
        {
            CatalogDataSet.CartRow cartRow = _catalogDataSet.Cart.FindByUserName(userName);
            CatalogDataSet.CartItemRow cartItemRow;

            if (cartRow == null)
            {
                _catalogDataSet.Cart.AddCartRow(userName);
                cartRow = _catalogDataSet.Cart.FindByUserName(userName);
            }

            cartItemRow = _catalogDataSet.CartItem.FindByCartIdItem(cartRow.CartId, itemId);

            if (cartItemRow == null)
            {
                CatalogDataSet.ItemRow itemRow = _catalogDataSet.Item.FindByItemId(itemId);
                _catalogDataSet.CartItem.AddCartItemRow(cartRow, itemRow, quantity);
            }
            else
            {
                cartItemRow.Quantity += quantity;
            }
        }

        public void RemoveItem(string userName, Item item, int quantity)
        {
            if (item == null)
                return;

            CatalogDataSet.CartRow cartRow = _catalogDataSet.Cart.FindByUserName(userName);
            CatalogDataSet.CartItemRow cartItemRow = _catalogDataSet.CartItem.FindByCartIdItem(cartRow.CartId, item.ItemId);

            if (cartItemRow == null)
                return;

            cartItemRow.Quantity -= quantity;

            if (cartItemRow.Quantity <= 0)
            {
                _catalogDataSet.CartItem.RemoveCartItemRow(cartItemRow);

                CatalogDataSet.CartItemRow[] cartItemRows = cartRow.GetCartItemRows();
                if (cartItemRows.Length == 0)
                    _catalogDataSet.Cart.RemoveCartRow(cartRow);
            }
        }

        public void DeleteCart(string userName)
        {
            CatalogDataSet.CartRow cartRow = _catalogDataSet.Cart.FindByUserName(userName);

            foreach (CatalogDataSet.CartItemRow cartItemRow in cartRow.GetCartItemRows())
                cartItemRow.Delete();

            cartRow.Delete();
        }

        private Cart TranslateFromCartRowToCartEntity(CatalogDataSet.CartRow cartRow)
        {
            Cart cart = new Cart();

            cart.CartId = cartRow.CartId;
            cart.User = cartRow.UserName;

            CatalogDataSet.CartItemRow[] cartItemRows = cartRow.GetCartItemRows();

            foreach (CatalogDataSet.CartItemRow cartItemRow in cartItemRows)
            {
                CartItem cartItem = TranslateFromCartItemRowToCartItemEntity(cartItemRow);
                cart.CartItems.Add(cartItem);
            }

            return cart;
        }

        private CartItem TranslateFromCartItemRowToCartItemEntity(CatalogDataSet.CartItemRow cartItemRow)
        {
            CartItem cartItem = new CartItem();

            cartItem.CartId = cartItemRow.CartId;
            cartItem.Item = _itemsService.GetItemById(cartItemRow.Item);
            cartItem.Quantity = cartItemRow.Quantity;

            return cartItem;
        }
    }
}
